package com.polaris.live.widget.pag

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import androidx.constraintlayout.widget.ConstraintLayout
import com.polaris.live.R
import com.polaris.live.common.constant.PagConst
import com.polaris.live.common.manager.CoroutineScopeManager
import com.polaris.live.databinding.WidgetFollowBinding
import com.polaris.live.utils.file.FileUtils
import com.polaris.live.utils.image.ImageUtils
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.libpag.PAGFile
import org.libpag.PAGImage
import org.libpag.PAGView
import org.libpag.PAGView.PAGViewListener

/**
 * AddFollowView
 *
 * @author Created by 半仙 on 2024/1/9/009 9:29
 */
class AddFollowView(context: Context, attrs: AttributeSet? = null) : ConstraintLayout(context, attrs) {

    private val mBinding = WidgetFollowBinding.inflate(LayoutInflater.from(context), this, true)

    private var job: Job? = null
    private var pagFile: PAGFile? = null

    companion object {

        const val LIVE_ADD_FOLLOWE = PagConst.LIVE_ADD_FOLLOWE
        const val LIVE_ADD_FOLLOWE_ALPHA = PagConst.LIVE_ADD_FOLLOWE_ALPHA
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)

        val width = MeasureSpec.getSize(widthMeasureSpec)
        val height = MeasureSpec.getSize(heightMeasureSpec)
        val layoutParams = mBinding.root.layoutParams
        layoutParams.width = width
        layoutParams.height = height
        mBinding.root.layoutParams = layoutParams
    }

    fun setPathType(pathType: String) {
        job = CoroutineScopeManager.ioScope.launch {
            try {
                val filePath = FileUtils.getAssetsToFilePag(pathType)
                pagFile = PAGFile.Load(filePath)
                if (pathType == LIVE_ADD_FOLLOWE) {
                    val bitmap = ImageUtils.getBitmapFromDrawable(context, R.drawable.live_add_followe_back)
                    val pagImage = PAGImage.FromBitmap(bitmap)
                    pagFile?.replaceImage(2, pagImage)
                } else {
                    val bitmapBack = ImageUtils.getBitmapFromDrawable(context, R.drawable.live_add_followe_back)
                    val pagImageBack = PAGImage.FromBitmap(bitmapBack)
                    pagFile?.replaceImage(2, pagImageBack)
                }
                val bitmapYes = ImageUtils.getBitmapFromDrawable(context, R.drawable.live_add_followe_alpha_yes)
                val pagImageYes = PAGImage.FromBitmap(bitmapYes)
                pagFile?.replaceImage(0, pagImageYes)

                val bitmapAdd = ImageUtils.getBitmapFromDrawable(context, R.drawable.live_add_followe_alpha_add)
                val pagImageAdd = PAGImage.FromBitmap(bitmapAdd)
                pagFile?.replaceImage(1, pagImageAdd)
                withContext(CoroutineScopeManager.mainDispatcher) {
                    mBinding.pagFollow.composition = pagFile
                    mBinding.pagFollow.flush()
                }
            } finally {
                if (coroutineContext[Job] == job) {
                    job = null
                }
            }
        }
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        job?.cancel()
        job = null
    }

    fun addListener(pagListener: com.polaris.live.interface_bag.PAGViewListener) {
        mBinding.pagFollow.addListener(object : PAGViewListener {
            override fun onAnimationStart(p0: PAGView?) {
                pagListener.onAnimationStart(p0)
            }

            override fun onAnimationEnd(p0: PAGView?) {
                pagListener.onAnimationEnd(p0)
                mBinding.pagFollow.composition = pagFile
                mBinding.pagFollow.progress = 0.1
                mBinding.pagFollow.flush()
            }

            override fun onAnimationCancel(p0: PAGView?) {
                pagListener.onAnimationCancel(p0)
            }

            override fun onAnimationRepeat(p0: PAGView?) {
                pagListener.onAnimationRepeat(p0)
            }

            override fun onAnimationUpdate(p0: PAGView?) {
                pagListener.onAnimationUpdate(p0)
            }

        })
    }

    fun setRepeatCount(count: Int) {
        mBinding.pagFollow.setRepeatCount(count)
    }

    fun setScaleMode(pagMode: Int) {
        mBinding.pagFollow.setScaleMode(pagMode)
    }

    fun play() {
        pagFile?.let {
            mBinding.pagFollow.composition = it
        }
        mBinding.pagFollow.play()
    }

}